<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <title>运动营养新增或修改</title>

    <link rel="stylesheet" href="../res/layui/css/layui.css" media="all">
    <script type="text/javascript" src="../res/js/tools.js"></script>
    <script type="text/javascript" src="../res/js/jquery-3.4.1.min.js"></script>
    <script src="../res/layui/layui.js"></script>
    <style type="text/css">
        #main {
            margin: 10px;
        }

        body {
            padding-top: 5px;
        }
    </style>

    <script type="text/javascript">
        //全局同步
        $.ajaxSetup({
            async: false
        });
        function getQueryString(name){
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]); return null;}
            var id = getQueryString("id");
        /* alert(id);*/

        $(function(){

            $("#schemeId").val(id);//和模型类一样

            //教练下拉类表框
            var url = "../../AtsSchemeActionNutrition/findAll";
            var data = null;
            $.post(url,data,function(obj){
                $.each(obj,function(index,item){
                    console.info(item);
                    $("#coachId").append("<option value='"+item.coachId+"'>"+item.coachName+"</option>");
                });
            },"json");


          /*  $("#coachId").onchange(function(){
                alert("asd");
            })
            /!*$("#coachId").on("onchange",function(){
                alert("asd");
            })*!/
            */


           /* /!**
             批量删除
             *!/
            $("#btnDelete").on("click",function(){
                var rolesId = "";
                $("input[name='roles']:checked").each(function(index,item){

                    rolesId+=$(this).val()+",";

                });

                if(rolesId==""){
                    alert("请选择一行记录!");
                    return ;
                }

                if(confirm("确定需要删除吗?")){
                    var url = "RolesContiller/select.action";
                    var data ={rolesId:rolesId};
                    $.post(url,data,function(obj){
                        alert(rolesId);
                    })
                }
            });*/



        });


        function conveterParamsToJson(paramsAndValues) {
            var jsonObj = {};

            var param = paramsAndValues.split("&");
            for ( var i = 0; param != null && i < param.length; i++) {
                var para = param[i].split("=");
                jsonObj[para[0]] = para[1];
            }

            return jsonObj;
        }


        /**
         * 将表单数据封装为json
         * @param form
         * @returns
         */
        function getFormData(form) {
            var formValues = $("#" + form).serialize();

            //关于jquery的serialize方法转换空格为+号的解决方法
            formValues = formValues.replace(/\+/g," ");   // g表示对整个字符串中符合条件的都进行替换
            var temp =  decodeURIComponent(JSON.stringify(conveterParamsToJson(formValues)));
            var queryParam = JSON.parse(temp);
            return queryParam;
        }

    </script>
</head>
<body>
<form class="layui-form" action="" id="YuangYuX">
    <input type="hidden" name="schemeId" id="schemeId"/>
    <div class="layui-form-item">
        <label class="layui-form-label">方案名称</label>
        <div class="layui-input-block">
            <input type="text" name="schemeName" id="schemeName" required
                   lay-verify="required" placeholder="方案名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">方案年度</label>
        <div class="layui-input-inline">
            <input type="year" id="year"  name="year" lay-verify="year" class="layui-input" placeholder="请输入年份">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">方案月份</label>
        <div class="layui-input-inline">
            <input type="number" id="month" name="month" lay-verify="month" class="layui-input" min=1 max=12 placeholder="请输入月份">
        </div>
    </div>


   <!-- <div class="layui-form-item">
        <label class="layui-form-label">教练员姓名</label>
        <div class="layui-input-block">
            <input type="hidden" name="coachName" id="coachName">
            <select name="coachId" lay-search="" id="coachId">
                <option value="">直接选择或搜索选择</option>
            </select>
        </div>
    </div>-->


    <div class="layui-form-item">
        <label class="layui-form-label">运动员</label>
        <div class="layui-input-block" id="fxk">

        </div>
        <input type="hidden" name="ids" id="ids">
        <input type="hidden" name="aName" id="athleteName">
    </div>







   <!-- <div class="layui-form-item">
        <label class="layui-form-label">附件1</label>
        <div class="layui-input-block" style="width:66%;">
            <input type="button" value="上传文件" onclick="pathone.click()" style="width:90px;height:40px;border:1px solid #ccc;background:#009688;color:#FFFFFF;border:none;border-radius:2px;">
            <input type="file" id="pathone" style="display:none" onchange="upfileone.value=this.value" name="fileOne" lay-verify="file">
            <input disabled type="text" size="30" name="upfileone" id="upfileone" style="border:none;background:#fff;">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">附件2</label>
        <div class="layui-input-block" style="width:66%;">
            <input type="button" value="上传文件" onclick="pathtwo.click()" style="width:90px;height:40px;border:1px solid #ccc;background:#009688;color:#FFFFFF;border:none;border-radius:2px;">
            <input type="file" id="pathtwo" style="display:none" onchange="upfiletwo.value=this.value" name="fileTwo">
            <input disabled type="text" size="30" name="upfiletwo" id="upfiletwo" style="border:none;background:#fff">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">附件3</label>
        <div class="layui-input-block" style="width:66%;">
            <input type="button" value="上传文件" onclick="paththree.click()" style="width:90px;height:40px;border:1px solid #ccc;background:#009688;color:#FFFFFF;border:none;border-radius:2px;">
            <input type="file" id="paththree" style="display:none" onchange="upfilethree.value=this.value" name="fileThree">
            <input disabled type="text" size="30" name="upfilethree" id="upfilethree" style="border:none;background:#fff">
        </div>
    </div>-->


    <!-- <div class="layui-form-item">
      <div id="tp">
      </div>
      <label class="layui-form-label" id="Divimg">附件1:</label>
      <div class="layui-input-block">
          <input class="input-file" name="fileOne" id="fileOne"
                 type="file" lay-verify="file">
      </div>
  </div>-->



  <!--  <div class="layui-form-item">
         <div id="tp2">

         </div>
         <label class="layui-form-label" id="Divimg2">附件2:</label>
         <div class="layui-input-block">
             <input class="input-file" name="fileTwo" id="fileTwo"
                    type="file">
         </div>
     </div>-->


  <!--   <div class="layui-form-item">
         <div id="tp3">

         </div>
         <label class="layui-form-label" id="Divimg3">附件3:</label>
         <div class="layui-input-block">
             <input class="input-file" name="fileThree" id="fileThree"
                    type="file">
         </div>
     </div>-->

    <div class="layui-form-item">
        <label class="layui-form-label">附件1</label>
        <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-normal" id="test8">选择文件</button>
            <input type="text" disabled readonly="readonly" id="upfileone" lay-verify="file" style="width:300px;background: #ffffff;border:none">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">附件2</label>
        <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-normal" id="test9">选择文件</button>
            <input type="text" disabled readonly="readonly" id="upfiletwo" style="width:300px;background: #ffffff;border:none">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">附件3</label>
        <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-normal" id="test10">选择文件</button>
            <input type="text" disabled readonly="readonly" id="upfilethree" style="width:300px;background: #ffffff;border:none">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script>


    /**
     * 新增和修改
     */
    var form = null ;
    layui.use(['form','laydate','layedit','upload'], function(){
        form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload;
        //判断文件上传不能为空
        var layedit = layui.layedit;

        //年选择器
        laydate.render({
            elem: '#year'
            ,type: 'year'
        });



        //判断文件上传不能为空
        var id=getQueryString("id");
        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //如果方案ID为空 新增验证附件不能为空，否则不需要验证
        if(id==null||id===""){
            //自定义验证规则
            form.verify({
                file: function(value){
                    if(value.length<=0){
                        return '附件1不能为空哦';
                    }
                }
                ,content: function(value){
                    layedit.sync(editIndex);
                }
            });
        }



        //监听提交
        form.on('submit(formDemo)', function(data){
            //layer.msg(JSON.stringify(data.field));

            //获取到下拉框选中教练员的文本
            var vs = $('#coachId option:selected').text();
            $("#coachName").val(vs);



            //jquery获取复选框值
            var rolesId = "";
            var athleteName="";
            $("input[name='roles']:checked").each(function(index,item){
                rolesId+=$(this).val()+",";
                athleteName+=$(this).attr("title")+",";
            });
            $("#ids").val(rolesId);
            $("#athleteName").val(athleteName);


            var formData = new FormData(document.getElementById("YuangYuX"));
            var url = "../../AtsSchemeActionNutrition/addinsert";
            $.ajax({
                url:url,
                type:"post",
                data:formData,
                processData:false,
                contentType:false,
                dataType:"json",
                success:function(obj){
                    /*alert("成功了");*/
                    layer.msg(obj.msg, {
                        icon: 1,
                        time: 1000 //1秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        //关闭当前层
                        //当你在iframe页面关闭自身时
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        //刷新父页面
                        //parent.location.reload();
                    });
                },
                error:function(e){
                    layer.msg("操作错误!");
                }
            });
            return false;
        });
        findlx();
       selectjs();
       findjs();

        upload.render({
            elem: '#test8'
            ,auto: false
            ,accept: 'file'
            ,field:'fileOne'
            ,choose: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                obj.preview(function(index, file, result){
                    $("#upfileone").val(file.name);
                });
            }
            ,done: function(res){
                console.log(res)
            }
        });
        upload.render({
            elem: '#test9'
            ,auto: false
            ,accept: 'file'
            ,field:'fileTwo'
            ,choose: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                obj.preview(function(index, file, result){
                    $("#upfiletwo").val(file.name);
                });
            }
            ,done: function(res){
                console.log(res)
            }
        });
        upload.render({
            elem: '#test10'
            ,auto: false
            ,accept: 'file'
            ,field:'fileThree'
            ,choose: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                obj.preview(function(index, file, result){
                    $("#upfilethree").val(file.name);
                });
            }
            ,done: function(res){
                console.log(res)
            }
        });
    });

    /**
     * 修改显示
     */
    function findlx(){
        var id = getQueryString("id");
        if(id){
            var url = "../../AtsSchemeActionNutrition/select";
            var data = {schemeId:id};
            //jquery的ajax方法,查询新闻类型的方法
            $.get(url,data,function(obj){
                //从后台传过来的数据obj$("[value='11']").prop("checked",true);
                $("#schemeName").val(obj.schemeName);
                $("#year").val(obj.year);
                $("#month").val(obj.month);
                $("#coachId").val(obj.coachId);
                $("#coachName").val(obj.coachName);
                $("#upfileone").val(obj.file1);
                $("#upfiletwo").val(obj.file2);
                $("#upfilethree").val(obj.file3);
                //重新渲染
                form.render();
            },"json");
        }

    }


    //复选框默认赋值
    function findjs(){
        var id = getQueryString("id");
        var url = "../../AtsSchemeActionNutrition/selectTwo";
        var data = {'id':id};
        //jquery的ajax方法,查询新闻类型的方法
        $.get(url,data,function(obj){
            $.each(obj,function(index,item){
                $("#ath_"+item.athleteId).prop("checked",true);
            });
            form.render();
        },"json");
    }

    /**
     * 复选框
     */
    function selectjs(){

        var url = "../../AtsSchemeActionNutrition/athleteselect";
        var data = {};
        //jquery的ajax方法,查询新闻类型的方法
        $.get(url,data,function(obj){
            //从后台传过来的数据obj$("[value='11']").prop("checked",true)
            $.each(obj,function(index,item){
                $("#fxk").append("<input type='checkbox' name='roles' value='"+item.athleteId+"' title='"+item.athleteName+"' id='ath_"+item.athleteId+"'>");
            });
            //重新渲染
            form.render();
        },"json");
    }

</script>



</body>
</html>